package com.victor9ac.tfm.flickr.adapter;


import java.util.List;

import com.flickr4java.flickr.FlickrException;
import com.flickr4java.flickr.photos.Photo;
import com.victor9ac.mashup.api.Image;
import com.victor9ac.mashup.api.Images;
import com.victor9ac.mashup.port.ImageOperation;
import com.victor9ac.tfm.flickr.client.FlickrClient;

public class FlickrAdapter implements ImageOperation {

	
	FlickrClient flickrClient;
	
	public Images getImageFromKeyword(String keyword) {
		
		List<Photo> flickrPhotoList  = flickrRequest(keyword);		
		
		Images images = adaptToMashup(flickrPhotoList);
		
		images.setKeywordLink(keyword);
		
		return images;
	}

	private List<Photo> flickrRequest(String keyword) {
		
		List<Photo> flickrPhotoList  = null;
		
		try {
			
			flickrPhotoList = flickrClient.getPhotos(keyword);
			
		} catch (FlickrException e) {
			// TODO Auto-generated catch block
			//FIXME add logger
			e.printStackTrace();
		}
		return flickrPhotoList;
	}

	private Images adaptToMashup(List<Photo> flickrPhotoList) {
		
		Images images = null;
		
		if (flickrPhotoList != null){
			images = new Images();
			for (Photo photoFlickr : flickrPhotoList) {
				Image img = new Image();
			
				img.setTittle(photoFlickr.getTitle());
				img.setUrl(photoFlickr.getSmallUrl());
				img.setDescription(photoFlickr.getDescription());
				
				images.add(img);
			}
			
		}
		return images;
	}

	public void setFlickrClient(FlickrClient flickrClient) {
		this.flickrClient = flickrClient;
	}

}
